#include "BinaryNode.h"

//Constructor, creates a binary node with data s and null left and right nodes - From Schwartz
BinaryNode::BinaryNode(string s) : data(s) 
{
	left=NULL;
	right=NULL;
}

//Copy Constructor
BinaryNode::BinaryNode(const BinaryNode& other) : data(other.data)
{
	left = NULL;
	right = NULL;

	if(other.left)
	{
		left = new BinaryNode(*(other.left));
	}
	if(other.right)
	{
		right = new BinaryNode(*(other.right));
	}
}

//Gets the data in this node - From Schwartz
string BinaryNode::getData() const { return data; }

//Gets the left node - From Schwartz
BinaryNode* BinaryNode::getLeft() const { return left; }

//Gets the right node - From Schwartz
BinaryNode*	BinaryNode::getRight() const { return right; }

//Sets the left node to another node, l - From Schwartz
void BinaryNode::setLeft(BinaryNode* l) { left = l; }

//Sets the right node to another node, r - From Schwartz
void BinaryNode::setRight(BinaryNode* r) { right = r; }

//Prints the tree recursively - From Schwartz, but fixed so that it actually works when one leaf is null but the other isn't
string BinaryNode::ToTree(string blank, string spacing) 
{
    string s = "" + data + '\n';
    if (left == NULL) 
	{
        s += spacing; s+= "null\n";
	}
	else 
	{
        s += spacing; s+= left->ToTree(blank, blank + spacing);
	}
	if (right == NULL) 
	{
        s += spacing; s+= "null\n";
	}
	else 
	{
        s += spacing; s+= right->ToTree(blank, blank + spacing);
	}
    return s;
}

//Recursively returns true if any node contains the target data - From Schwartz
bool BinaryNode::naivefind(string target, BinaryNode* n) 
{
    if (n == NULL)
        return false;
    else
        return target==n->data || naivefind(target, n->left) || naivefind(target, n->right);
}

//Sets left and right equal to null for use with removing nodes
void BinaryNode::isolateNode()
{
	left = NULL;
	right = NULL;
}

//Recursively deletes the node and all sub-nodes
BinaryNode::~BinaryNode()
{
	if(left)
		delete left;
	if(right)
		delete right;
}
